package booking.pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import static booking.utils.Locators.*;

public class HomePage extends BasePage {

	public HomePage(WebDriver driver) {
		super(driver);
	}

	public LoginPage navigateToLoginPage() {
		btnNavigateToUserControlPanel = driver.findElement(btnNavigateToUserControlPanelLocator);
		wait.until(ExpectedConditions.visibilityOf(btnNavigateToUserControlPanel));
		btnNavigateToUserControlPanel.click();
		return new LoginPage(driver);
	}
	
	public String getDisplayedLogin() {
		wait.until(ExpectedConditions.visibilityOfElementLocated(displayedLoginLocator));
		displayedLogin = driver.findElement(displayedLoginLocator);
		return displayedLogin.getText();
	}
	
	public HomePage isAtHomePage() {
		searchButton = driver.findElement(btnSearchButtonLocator);
		wait.until(ExpectedConditions.visibilityOf(searchButton));
		return this;
	}
	
	public HomePage inputFrom() {
		inputFrom = driver.findElement(inputFromLocator);
		wait.until(ExpectedConditions.visibilityOf(searchButton));
		return this;
	}
	
	public HomePage searchForTickets(String fromStation, String toStation, String date, int fromTime) throws InterruptedException {
		wait.until(ExpectedConditions.presenceOfElementLocated(inputFromLocator));
		inputFrom = driver.findElement(inputFromLocator);
		
		wait.until(ExpectedConditions.presenceOfElementLocated(inputToLocator));
		inputTo = driver.findElement(inputToLocator);
		
		wait.until(ExpectedConditions.presenceOfElementLocated(inputDepartureDateLocator));
		inputDepartureDate = driver.findElement(inputDepartureDateLocator);
		
		wait.until(ExpectedConditions.presenceOfElementLocated(selectDepartureTimeFromLocator));
		selectDepartureTimeFrom = new Select(driver.findElement(selectDepartureTimeFromLocator));
		
		wait.until(ExpectedConditions.presenceOfElementLocated(bntSearchLocator));
		btnSearch = driver.findElement(bntSearchLocator);
		
		inputFrom.click();
		inputFrom.clear();
		inputFrom.sendKeys(fromStation);
		wait.until(ExpectedConditions.visibilityOfElementLocated(dropdownFromLocator));
		dropdownFrom = driver.findElement(dropdownFromLocator);
		dropdownFrom.click();
		
		inputTo.click();
		inputTo.clear();
		inputTo.sendKeys(toStation);
		wait.until(ExpectedConditions.visibilityOfElementLocated(dropdownToLocator));
		dropdownTo = driver.findElement(dropdownToLocator);
		dropdownTo.click();
		
		inputDepartureDate.click();
		inputDepartureDate.clear();
		inputDepartureDate.sendKeys(date);
		
		selectDepartureTimeFrom.selectByIndex(fromTime);
		
		wait.until(ExpectedConditions.elementToBeClickable(bntSearchLocator));
		btnSearch.click();
		
		return this;
	}
	
	public HomePage isResNotFoundDisplayed(){
		wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(txtResNotFoundLocator));
		txtResNotFound = driver.findElement(txtResNotFoundLocator);
		return this;
	}
	
	public String getResNotFoundText(){
		txtResNotFound = driver.findElement(txtResNotFoundLocator);
		String displayedText = txtResNotFound.getText();
		return displayedText;
	}
	
	public HomePage isResultsTableDisplayed() {
		wait.until(ExpectedConditions.visibilityOfElementLocated(tableResultsLocator));
		return this;
	}
	
	private WebElement btnNavigateToUserControlPanel;
	private WebElement displayedLogin;
	private WebElement searchButton;
	private WebElement inputFrom;
	private WebElement dropdownFrom;
	private WebElement dropdownTo;
	private WebElement inputTo;
	private WebElement inputDepartureDate;
	private Select selectDepartureTimeFrom;
	private WebElement btnSearch;
	private WebElement txtResNotFound;
//	private WebElement tableResults;
}
